package cn.ainit.bs.report;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.ainit.bj.client.Tuser;
import cn.ainit.bs.repList.entity.RepListButtons;
import cn.ainit.bs.repList.entity.RepListColumns;
import cn.ainit.bs.repList.entity.RepListCondition;
import cn.ainit.bs.repList.entity.RepListDataStore;
import cn.ainit.bs.repList.entity.RepViewType;
import cn.ainit.bs.report.entity.ReportBar;
import cn.ainit.bs.report.entity.ReportPie;
import cn.ainit.sys.common.dao.Pagination;
import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryJson;
@Repository("reportDaoImpl")
public class ReportDaoImpl extends SysBaseDao implements ReportDao {
	
	
	@Override
	public ReportBar findRCOById(String reId) throws Exception {
		return (ReportBar) this.getSessionFactory().getCurrentSession().get(ReportBar.class, reId);
	}
	
	//根据sql语句和默认显示的行数显示数据列表
	@Override
	public List queryRpData(String sql, Integer xshowCount) throws Exception {
		System.out.println(sql);
		List datas=this.jdbcTemplate.queryForList(sql);
		return datas;
	}
	

	/***
	 * 根据dsId查询到报表的对象
	 * @param dsId
	 * @return
	 * @throws Exception
	 */
	@Override
	public RepListDataStore findByDsId(String dsId) throws Exception{
		return (RepListDataStore) this.getSessionFactory().getCurrentSession().get(RepListDataStore.class, dsId);
	}

	/**
	 * 根据dsId得到要显示的列
	 * @param dsId
	 * @return
	 */
	@Override
	public List getReportCols(String dsId, String type, String flag) throws Exception {
		List columns = new ArrayList();
		try {
			if (dsId == null || dsId.equals(""))throw new RuntimeException("DS_ID 传入有误");
			//列表界面
			String sql= "select ORDERNUM,READONLY,REQUIRED,DEFAULT_VALUE,X,Y,WIDTH,HEIGHT,FIRSTPAGEFLAG,SECPAGEFLAG,SINGLE_ROW,ISSHOW,USE_BEFORE_VALUE,CUSTOM_PROPS,JAVA_CLASS,"+
			"JAVA_PROPERTY,IS_OPEN_LOAD_SAME,OPEN_LOAD_TYPE,LOAD_INIT_DATA_FUN,MEMO,IS_PK,CONFIG,FORMAT_CONT,"+
			"COMMON_USE,DS_ID,NAME,COLN_NAME,GROUP_NAME,PARENT_GROUP,GROUP_ID,GROUP_IS_OPEN,FORMAT,INPUTTYPE,EDITYPE,JAVA_PROPERTY_RECORDED,"+
			"HIDDEN,STYLER,ONCLICK,EDITOR FROM REPORT_COLUMNS WHERE DS_ID = '"+dsId+"' AND ISSHOW='T' AND JAVA_PROPERTY_RECORDED IS NOT NULL ORDER BY ORDERNUM";
			if (type != null && !type.equals("")) {
				columns = this.getJdbcTemplate().queryForList(sql);
			}
			this.getJdbcTemplate().queryForList(sql);
		} catch (Exception e) {
			throw e;
		}
		return columns;
	}
	
	
	/**
	 * 根据dsId和视图的筛选条件得到要显示的列
	 * @param dsId
	 * @return
	 */
	@Override
	public List getReportCol(String dsId,String repChoise,String type, String flag) throws Exception {
		List columns = new ArrayList();
		try {
			if (dsId == null || dsId.equals(""))throw new RuntimeException("DS_ID 传入有误");
			String sql="select r.ORDERNUM,r.READONLY,r.REQUIRED,r.DEFAULT_VALUE,r.X,r.Y,r.WIDTH,r.HEIGHT,r.FIRSTPAGEFLAG,r.SECPAGEFLAG,r.SINGLE_ROW,r.ISSHOW,r.USE_BEFORE_VALUE,r.CUSTOM_PROPS,r.JAVA_CLASS,"+
      		"r.JAVA_PROPERTY,r.IS_OPEN_LOAD_SAME,r.OPEN_LOAD_TYPE,r.LOAD_INIT_DATA_FUN,r.MEMO,r.IS_PK,r.CONFIG,r.FORMAT_CONT,"+
      		"r.COMMON_USE,r.DS_ID,r.NAME,r.COLN_NAME,r.GROUP_NAME,r.PARENT_GROUP,r.GROUP_ID,r.GROUP_IS_OPEN,r.FORMAT,r.INPUTTYPE,r.EDITYPE,r.JAVA_PROPERTY_RECORDED,"+
      		"r.HIDDEN,r.STYLER,r.ONCLICK,r.EDITOR FROM REPORT_COLUMNS r ,report_condition c WHERE r.ds_id = '"+dsId+"' AND r.ISSHOW='T' and r.ds_id=c.ds_id and c.name='H_NAME_A'"+
      		"and r.JAVA_PROPERTY_RECORDED in(select t.colid from report_condition_cols t where (t.is_common='T' and t.ds_id='"+dsId+"')  or (t.ds_id='"+dsId+"' and t.name='"+repChoise+"'))  ORDER BY ORDERNUM";
			if (type != null && !type.equals("")) {
				columns = this.getJdbcTemplate().queryForList(sql);
			}
			this.getJdbcTemplate().queryForList(sql);
		} catch (Exception e) {
			throw e;
		}
		return columns;
	}
	

	
	/**
	 *  根据dsId得到要显示的按钮
	 * @param dsId
	 * @return
	 */
	@Override
	public List getReportButns(String dsId) throws Exception {
		List<RepListButtons> butnsButtons=new ArrayList<RepListButtons>();
		String sql="from RepListButtons where dsid = ?";
		butnsButtons=(List<RepListButtons>) this.getSessionFactory().getCurrentSession().createQuery(sql).setString(0, dsId).list();
		return butnsButtons;
	}
	
	/**
	 *  根据dsId得到要报表的条件
	 * @param dsId
	 * @return
	 */
	@Override
	public List getReportCondition(String dsId) throws Exception {
		List<RepListCondition> rcs=new ArrayList<RepListCondition>();
		String sql="from RepListCondition where dsid = ?";
		rcs=(List<RepListCondition>) this.getSessionFactory().getCurrentSession().createQuery(sql).setString(0, dsId).list();
		return rcs;
	}

	

	/***
	 * 更新数据
	 * @param sqlText
	 * @param cpage
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@Override
	public QueryJson queryRowsBySqlPagination(String sqlText, int cpage,
			int rows) throws Exception {
		QueryJson queryJson = new QueryJson();
		try {
			//dbType代表数据库的类型
			//cpage当前页
			//rows每页的记录数
			Pagination pagination = new Pagination(dbType, sqlText, cpage, rows, jdbcTemplate);
			queryJson.setTotal(pagination.getTotalRows());   //得到总的页数
			queryJson.setRows(pagination.getResultList());  //分页显示的结果集
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return queryJson;
	}

	@Override
	public List getCommonData(String dsId) throws Exception {
		List<RepViewType> commonDatas =new ArrayList<RepViewType>();
		try {
			if (dsId == null || dsId.equals(""))throw new RuntimeException("DS_ID 传入有误");
			String hql="from RepViewType where isCommon='T' and id.dsId=?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, dsId);
			commonDatas = query.list();
		} catch (Exception e) {
			throw e;
		}
		return commonDatas;
	}

	@Override
	public RepViewType getViewChoice(String dsId, String viewName) throws Exception {
		RepViewType data = new RepViewType();
		try {
			if (dsId == null || dsId.equals(""))throw new RuntimeException("DS_ID 传入有误");
			String hql="from RepViewType where id.name=? and id.dsId=?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, viewName);
			query.setString(1, dsId);
			List<RepViewType> list = query.list();
			if (list != null && list.size() > 0) {
				data=list.get(0);
				return data;
			}
		} catch (Exception e) {
			throw e;
		}
		return data;
	}
	
	@Override
	public List loadRresult(StringBuffer sql, Integer currentPage,
			Integer pageSize) throws Exception {
		Pagination pagination = new Pagination(dbType, sql.toString(), currentPage, pageSize, this.getJdbcTemplate());
		return pagination.getResultList();
	}
	
	
	@Override
	public ReportBar findBarByReId(String reid) throws Exception {
		return (ReportBar) this.getSessionFactory().getCurrentSession().get(ReportBar.class, reid);
	}
	
	@Override
	public ReportPie findPieByReId(String reid) throws Exception {
		return (ReportPie)this.getSessionFactory().getCurrentSession().get(ReportPie.class, reid);
	}
}
